package com.fjh.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjh.domain.UserCollection;
import com.fjh.es.ProdEs;
import com.fjh.service.UserCollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("p/collection")
@Api(tags = "用户收藏管理")
public class UserCollectController {
    @Autowired
    private UserCollectionService userCollectionService;

    @GetMapping("count")
    @ApiOperation("查询用户的收藏数量")
    public ResponseEntity<Integer> getUserCollectCount(){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        Integer count = userCollectionService.getUserCollectCount(userId);
        return ResponseEntity.ok(count);
    }

    @GetMapping("prods")
    @ApiOperation("分页查询用户的收藏商品信息")
    public ResponseEntity<Page<ProdEs>> getUserCollectPage(Page<UserCollection> page){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        Page<ProdEs> prodEsPage = userCollectionService.getUserCollectPage(page,userId);
        return ResponseEntity.ok(prodEsPage);
    }

    @GetMapping("isCollection")
    @ApiOperation("查询用户是否收藏该商品")
    public ResponseEntity<Boolean> isCollection(Long prodId){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        Boolean flag = userCollectionService.isCollection(userId,prodId);
        return ResponseEntity.ok(flag);
    }

    @PostMapping("addOrCancel")
    @ApiOperation("添加或者取消收藏该商品")
    public ResponseEntity<Void> addOrCancel(@RequestBody Long prodId){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        userCollectionService.addOrCancel(userId,prodId);
        return ResponseEntity.ok().build();
    }


}
